import { NzMessageService } from 'ng-zorro-antd/message';
import { PhasedTaskApiService } from 'src/app/api/phased-task-api.service';
import { PeriodTaskApiService } from 'src/app/api/period-task-api.service';
import { Router } from '@angular/router';
import { ProcessStatus, Task, TaskClassfiy } from './../../../model/task';
import { Component, EventEmitter, Input, OnInit, Output, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-overview-task-list',
  templateUrl: './overview-task-list.component.html',
  styleUrls: ['./overview-task-list.component.less']
})
export class OverviewTaskListComponent implements OnInit {

  @Input() listTitle = '';
  @Input() titleBackgroundColor = '#fff';
  @Input() taskList: Array<Task> = [];
  @Output() listStatusChange = new EventEmitter<boolean>();
  constructor(
    private router: Router,
    private periodApi: PeriodTaskApiService,
    private phaseApi: PhasedTaskApiService,
    private msg: NzMessageService,
  ) { }

  ngOnInit(): void {
  }

  afterCheck = (task: any): void => {
    this.msg.success('工作状态修改成功');
    this.listStatusChange.emit(true);
  }
  checkErrorr = (err: any): void => {
    this.msg.error('工作状态修改错误');
    console.error(err);

  }
  taskCheck(task: Task): void {
    switch (task.classify) {
      case TaskClassfiy.period:
        this.periodApi.setTaskCurrentStatus(task.id, ProcessStatus.done).subscribe(
          this.afterCheck,
          this.checkErrorr
        );
        break;
      case TaskClassfiy.phase:
        this.phaseApi.updateTaskProcessStatus(task.id, ProcessStatus.done).subscribe(
          this.afterCheck,
          this.checkErrorr
        );
        break;

      default:
        break;
    }
  }
  taskDetial(task: Task): void {
    switch (task.classify) {
      case TaskClassfiy.period:
        const periodUrl = '/period/detail/' + task.id;
        this.router.navigateByUrl(periodUrl);
        break;
      case TaskClassfiy.phase:
        const phaseUrl = '/phaseTask/detail/' + task.id;
        this.router.navigateByUrl(phaseUrl);
        break;

      default:
        break;
    }
  }

}
